/*	@brief	:	mutex for threads
*	@author	:	yangcancheng
*	@date	:	2009-08-04
*/

#ifndef _CTHREAD_MUTEX_H_
#define _CTHREAD_MUTEX_H_

#include <assert.h>
#include <pthread.h>

namespace CThread
{

/**
* A simple mutex class
*
* @version $Id:$
*/
class Mutex
{
public:
	inline Mutex()
	{
		if (pthread_mutex_init(&_mtx, NULL) != 0 ) 
		{
			//assert(0);
		}
	}

	inline ~Mutex()
	{
		if(pthread_mutex_destroy(&_mtx) != 0) 
		{
			//assert(0);
		}
	}
	
	inline void lock()
	{
        pthread_mutex_lock(&_mtx);        
	}
	
	inline bool trylock() 
	{
		return (pthread_mutex_trylock(&_mtx) == 0);
	}
	
	inline void unlock() 
	{
		pthread_mutex_unlock(&_mtx);
	}

private:
	pthread_mutex_t _mtx;
};

};

#endif // #ifndef _CTHREAD_MUTEX_H_
